import type { DataTableColumns, DropdownOption } from 'naive-ui'
import { NDropdown } from 'naive-ui'
import departmentApi from '@/service/api/setting/department.ts'
import type { CreateColumsOptions } from '@/types/fcform'

export const createColums = ({ handDel, action }: CreateColumsOptions): DataTableColumns => {
  return [
    {
      title: '部门名称',
      key: 'name',
    },
    {
      title: '排序',
      key: 'weight',
      align: 'center',
      width: 80,
    },
    {
      title: '操作',
      key: 'total',
      align: 'center',
      width: 120,
      render(row) {
        const arr = []
        const addBtn = fastUnit.createBtn('添加', {
          type: 'primary',
          onClick: () =>
            action.default({
              parentId: row.id,
              name: '',
              sortOrder: 1,
            }),
        })

        if (commonUnit.auth('sys_dept_add')) {
          arr.push(addBtn)
        }
        const moreBtnOptions: DropdownOption[] = []
        if (commonUnit.auth('sys_dept_edit')) {
          moreBtnOptions.push({
            label: '编辑',
            key: 'edit',
          })
        }
        if (commonUnit.auth('sys_dept_del')) {
          moreBtnOptions.push({
            label: () =>
              h(
                'div',
                {
                  style: {
                    color: 'red',
                  },
                },
                '删除',
              ),
            key: 'del',
            disabled: row.username === 'admin',
          })
        }
        const moreBtn = h(
          NDropdown,
          {
            trigger: 'hover',
            options: moreBtnOptions,
            onSelect: (e: string) => {
              switch (e) {
                case 'edit':
                  departmentApi.detail(row.id).then(({ data }) => {
                    action.default(data)
                  })
                  break
                case 'del':
                  handDel(row)
                  break
              }
            },
          },
          {
            default: () => fastUnit.createBtn('更多'),
          },
        )
        if (moreBtnOptions.length > 0) {
          arr.push(moreBtn)
        }
        return h('div', arr)
      },
    },
  ]
}
